// Figure 3

// Load Data
use "all_tasklevel_data.dta", clear
drop if missing(sex)

label define dec_ 1 "1st Decile Break" 2 "2nd Decile Break" 3 "3rd Decile Break" 4 "4th Decile Break" ///
  5 "5th Decile Break" 6 "6th Decile Break" 7 "7th Decile Break" 8 "8th Decile Break" 9 "9th Decile Break" 10 "10th Decile Break" 11 "First Task of Day", add
  label values abs_tdiff_group dec_

label define dec_2 1 "1st Decile Session Task" 2 "2nd Decile Session Task" 3 "3rd Decile Session Task" 4 "4th Decile Session Task" ///
  5 "5th Decile Session Task" 6 "6th Decile Session Task" 7 "7th Decile Session Task" 8 "8th Decile Session Task" 9 "9th Decile Session Task" 10 "10th Decile Session Task", add
 label values cluster10_group dec_2
 
 label var same_task "Same Task at t-1"

sort year_a month_a
egen time_group = group(month_a year_a) 
gen quarter_group = 0

local index=1
forvalues i = 1(3)39 {
	replace quarter_group = `i' if time_group >= `i' & time_group <= `i'+3
	local index = `index'+1
}

g overlap = (time_diff < 0)

// Histograms of breaks and time diff

twoway (histogram abs_tdiff_group if in_sample==1 & returned==0 & sex==1 & abs_tdiff<11, discrete ///
			fcolor(gs10) lcolor(gs10)) ///
		(histogram abs_tdiff_group if in_sample==1 & returned==0 & sex==0 & abs_tdiff<11, discrete ///
			fcolor(none) lcolor(black)), ///
			legend(order(1 "Female" 2 "Male")) ///
			xtitle("Decile of Break Between Tasks") graphregion(color(white))

twoway (histogram cluster10_group if in_sample==1 & returned==0 & sex==1 & cluster10_group<11, discrete ///
			fcolor(gs10) lcolor(gs10)) ///
		(histogram cluster10_group if in_sample==1 & returned==0 & sex==0 & cluster10_group<11, discrete ///
			fcolor(none) lcolor(black)), ///
			legend(order(1 "Female" 2 "Male")) ///
			xtitle("Decile of Work Session Position") graphregion(color(white))

			
// Worker fixed effect regressions
xtset worker_nid 

foreach thing in abs_tdiff cluster10 {


	forvalues i = 0/1 {
		xtreg log_interval_wage log_task_reward i.task_type_ log_worker_n ///
					i.`thing'_group i.quarter_group  ///
					if in_sample==1 & returned==0 & sex==`i', fe vce(cluster worker_nid)
		estimates store fe`thing'_s`i'
		
		xtreg log_interval_wage log_task_reward i.task_type_ log_worker_n ///
					i.`thing'_group i.quarter_group  ///
					if in_sample==1 & returned==0 & sex==`i' & no_batch==0, fe vce(cluster worker_nid)		
		
		estimates store fe`thing'_s`i'_b0
		
		xtreg log_interval_wage log_task_reward i.task_type_ log_worker_n ///
					i.`thing'_group i.quarter_group  ///
					if in_sample==1 & returned==0 & sex==`i' & no_batch==1, fe vce(cluster worker_nid)	
		estimates store fe`thing'_s`i'_b1			
	}
	
	xtreg log_interval_wage log_task_reward i.task_type_ log_worker_n ///
					i.`thing'_group i.quarter_group  ///
					if in_sample==1 & returned==0 & no_batch==0, fe vce(cluster worker_nid)		
	estimates store fe`thing'_b0
	
	xtreg log_interval_wage log_task_reward i.task_type_ log_worker_n ///
					i.`thing'_group i.quarter_group  ///
					if in_sample==1 & returned==0 & no_batch==1, fe vce(cluster worker_nid)	
	estimates store fe`thing'_b1
	
}

foreach thing in abs_tdiff cluster10 {


	forvalues i = 0/1 {
		reg log_interval_wage log_task_reward i.task_type_ log_worker_n ///
					i.`thing'_group i.quarter_group  ///
					if in_sample==1 & returned==0 & sex==`i', vce(cluster worker_nid)
		estimates store re`thing'_s`i'
				
	}
	
	forvalues i = 0/1 {
		reg log_interval_wage log_task_reward i.task_type_ log_worker_n ///
					i.`thing'_group i.quarter_group  ///
					if in_sample==1 & returned==0 & no_batch==`i', vce(cluster worker_nid)
		estimates store re`thing'_b`i'
				
	}
		
}

// Export to make graphs in Matlab
foreach thing in abs_tdiff cluster10 {

	esttab fe`thing'_s0 fe`thing'_s1 using "main_sex_`thing'.csv" , ///
		b(4) se(4) label noparentheses nostar ///
		keep(*`thing'_group ) nonotes replace wide
		
	esttab fe`thing'_b0 fe`thing'_b1 using "main_batch_`thing'.csv" , ///
		b(4) se(4) label noparentheses nostar ///
		keep(*`thing'_group ) nonotes replace wide
  
	esttab fe`thing'_s0_b0 fe`thing'_s1_b0 fe`thing'_s0_b1 fe`thing'_s1_b1 ///
		using "appendix_batch_sex_`thing'.csv" , ///
		b(4) se(4) label noparentheses nostar ///
		keep(*`thing'_group ) nonotes replace wide
		
	esttab re`thing'_s0 re`thing'_s1 using "append_sex_`thing'.csv" , ///
		b(4) se(4) label noparentheses nostar ///
		keep(*`thing'_group ) nonotes replace wide
		
	esttab re`thing'_b0 re`thing'_b1 using "append_batch_`thing'.csv" , ///
		b(4) se(4) label noparentheses nostar ///
		keep(*`thing'_group ) nonotes replace wide		
  }

table abs_tdiff_group, c(median abs_time)
table cluster10_group, c(median cluster10_n)

// p-values 
g id_men = .
replace id_men = worker_nid if sex==0
g id_women = .
replace id_women = worker_nid if sex==1

set matsize 4000


foreach thing in abs_tdiff cluster10 {

	forvalues i = 0/1 {
	
		if `i' == 0 {
			local sgroup id_men
		}
		if `i' == 1 {
			local sgroup id_women
		}
	
		reg log_interval_wage log_task_reward i.task_type_ log_worker_n ///
					i.`thing'_group i.quarter_group i.`sgroup'  ///
					if in_sample==1 & returned==0 & sex==`i'
		estimates store fe`thing'_s`i'
		
		reg log_interval_wage log_task_reward i.task_type_ log_worker_n ///
					i.`thing'_group i.quarter_group i.`sgroup' ///
					if in_sample==1 & returned==0 & sex==`i' & no_batch==0		
		
		estimates store fe`thing'_s`i'_b0
		
		reg log_interval_wage log_task_reward i.task_type_ log_worker_n ///
					i.`thing'_group i.quarter_group i.`sgroup' ///
					if in_sample==1 & returned==0 & sex==`i' & no_batch==1
		estimates store fe`thing'_s`i'_b1	
		
	}

	reg log_interval_wage log_task_reward i.task_type_ log_worker_n ///
					i.`thing'_group i.quarter_group  i.worker_nid ///
					if in_sample==1 & returned==0 & no_batch==0		
	estimates store fe`thing'_b0
	
	reg log_interval_wage log_task_reward i.task_type_ log_worker_n ///
					i.`thing'_group i.quarter_group i.worker_nid ///
					if in_sample==1 & returned==0 & no_batch==1
	estimates store fe`thing'_b1

}


foreach thing in abs_tdiff cluster10 {

	qui suest fe`thing'_s0 fe`thing'_s1, vce(cluster worker_nid)
	test ([fe`thing'_s0_mean]2.`thing'_group = [fe`thing'_s1_mean]2.`thing'_group) ///
		([fe`thing'_s0_mean]3.`thing'_group = [fe`thing'_s1_mean]3.`thing'_group) ///
		([fe`thing'_s0_mean]4.`thing'_group = [fe`thing'_s1_mean]4.`thing'_group) ///
		([fe`thing'_s0_mean]5.`thing'_group = [fe`thing'_s1_mean]5.`thing'_group) ///
		([fe`thing'_s0_mean]6.`thing'_group = [fe`thing'_s1_mean]6.`thing'_group) ///
		([fe`thing'_s0_mean]7.`thing'_group = [fe`thing'_s1_mean]7.`thing'_group) ///
		([fe`thing'_s0_mean]8.`thing'_group = [fe`thing'_s1_mean]8.`thing'_group) ///
		([fe`thing'_s0_mean]9.`thing'_group = [fe`thing'_s1_mean]9.`thing'_group) 
}


foreach thing in abs_tdiff cluster10 {

	qui suest fe`thing'_b0 fe`thing'_b1, vce(cluster worker_nid)
	test ([fe`thing'_b0_mean]2.`thing'_group = [fe`thing'_b1_mean]2.`thing'_group) ///
		([fe`thing'_b0_mean]3.`thing'_group = [fe`thing'_b1_mean]3.`thing'_group) ///
		([fe`thing'_b0_mean]4.`thing'_group = [fe`thing'_b1_mean]4.`thing'_group) ///
		([fe`thing'_b0_mean]5.`thing'_group = [fe`thing'_b1_mean]5.`thing'_group) ///
		([fe`thing'_b0_mean]6.`thing'_group = [fe`thing'_b1_mean]6.`thing'_group) ///
		([fe`thing'_b0_mean]7.`thing'_group = [fe`thing'_b1_mean]7.`thing'_group) ///
		([fe`thing'_b0_mean]8.`thing'_group = [fe`thing'_b1_mean]8.`thing'_group) ///
		([fe`thing'_b0_mean]9.`thing'_group = [fe`thing'_b1_mean]9.`thing'_group) 
}

